home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / Toolbox / OpenWindowƒ / OpenWindow.p < prev    next >
Encoding:
Text File  |  1992-07-15  |  4.2 KB  |  117 lines  |  [TEXT/MPS ]

  1.  
  2. PROGRAM OpenWindow;
  3.  
  4. USES    MemTypes,SysEqu,OSUtils,ToolUtils,SegLoad,Fonts,Retrace,
  5.         QuickDraw,PackIntf,Resources,OSEvents,Events,Memory,Script;
  6.  
  7. VAR
  8.     WindTitle : Str255;
  9.     MyWindow,aWindPtr : WindowPtr;
  10.     err : integer;
  11.     MyEvent : EventRecord;
  12.     quit,DrawOn : boolean;
  13.     WindRect : Rect;
  14.     aPoint : point;
  15.     myCrsr : CursHandle;
  16.     
  17. {------------------------------------------------------------------------------------}
  18.  
  19. PROCEDURE _DataInit;    EXTERNAL;
  20.  
  21. {------------------------------------------------------------------------------------}
  22.  
  23. PROCEDURE InitMac;
  24.  
  25. BEGIN                       {InitMac}    {the big inits}
  26.  
  27.     UnLoadSeg(@_DataInit);               {remove data initialization code before any allocations}
  28.     InitGraf(@thePort);                {initialize QuickDraw}
  29.     InitFonts;                           {initialize Font Manager}
  30.     FlushEvents(everyEvent, 0);        {call OS Event Mgr to discard any previous events}
  31.     InitWindows;                       {initialize Window Manager}
  32.     InitCursor;                        {call QuickDraw to make cursor (pointer) an arrow}
  33.     quit := false;
  34.     DrawOn := false;
  35.  
  36. END;                       {InitMac}
  37.  
  38. {------------------------------------------------------------------------------------}
  39.  
  40. PROCEDURE DoDraw;
  41.  
  42. BEGIN
  43.     DrawOn := true;
  44.     GlobalToLocal (Myevent.where);
  45.     MoveTo (Myevent.where.h,Myevent.where.v);
  46.     EraseRect (WindRect);
  47. END;
  48.  
  49. {------------------------------------------------------------------------------------}
  50.  
  51. BEGIN                              {main PROGRAM}
  52.  
  53.     InitMac;                                {'nitialize them Mac Managers}
  54.  
  55.     {Now let's figure out how big the (main) screen is, and make a Rect that fits inside it}
  56.     {Screenbits.bounds always has the Rect of the main screen}
  57.     
  58.     WindRect.top := screenBits.bounds.top + GetMBarHeight+25;
  59.     WindRect.left := screenBits.bounds.left + 5;
  60.     WindRect.bottom := screenBits.bounds.bottom - 5;
  61.     WindRect.right := screenBits.bounds.right - 5;
  62.     
  63.     WindTitle := ('Press "q" to quit');        {the window title will tell the user how to quit this program}
  64.     
  65.     MyWindow := NewWindow                    {we'll make a window with NewWindow, cuz we don't want to carry}
  66.                                             { a resource around}
  67.                            (nil,            {let the window manager take care of storing the window record}
  68.                            WindRect,        {this is our "custom tailored to the scereen" rect}
  69.                            WindTitle,        {our famous "user interface in the window title" title}
  70.                            true,            {the visible boolean, we'd like to see our} 
  71.                            noGrowDocProc,    {this tells NewWindow what kind of window we want}
  72.                            Pointer(-1),        {this param is the pointer to the window we want to be behind}
  73.                                                { in this case, we want to be frontmost, and Pointer(-1) does that}
  74.                            true,            {this is true, so we can have a "go away" box in the title bar}
  75.                            1);                {refCon - a reference number; used by the app for whatever it wants to.}
  76.  
  77.     if MyWindow <> nil then                    {make sure the new window pointer is valid}
  78.     begin
  79.         SetPort (MyWindow);                    {set the current graf port to our new window}
  80.         GlobalToLocal (WindRect.topLeft);    {make our Window Rect local coords so erase rect will do the right thing}
  81.         GlobalToLocal (WindRect.botRight);
  82.         myCrsr := GetCursor (crossCursor);    {we'll use the crosshair cursor}
  83.         if myCrsr <> nil then
  84.         begin
  85.             MoveHHi (handle(myCrsr));        {move the cursor data high in the heap to avoid fragmentation}
  86.             HLock (handle(myCrsr));            {now lock the handle down}
  87.             SetCursor (cursor(myCrsr^^));    {and finally set the cursor to the crosshair}
  88.             repeat
  89.                 if not DrawOn then            {DrawOn is sort of a "mouse down in content region" flag}
  90.                 begin
  91.                     if GetNextEvent (EveryEvent,MyEvent) then    {only check for events when mouse is up}
  92.                     
  93.                         CASE MyEvent.what of
  94.                         
  95.                             mouseDown : if ((FindWindow (MyEvent.where,aWindPtr) = inContent)    {make sure the mouseDown is}
  96.                                                                                                 {where we want it}
  97.                                         and (aWindptr = MyWindow)) then DoDraw
  98.                                         else if FindWindow (MyEvent.where,aWindPtr) = inGoAway then Quit := true;
  99.                         
  100.                             KeyDown : if (char(BitAnd (MyEvent.message,charCodeMask)) = 'q') then quit := true;
  101.                             
  102.                         end; {case}
  103.                 end
  104.                 else
  105.                 begin
  106.                     If StillDown then        {if mouse btn is still down, keep drawing}
  107.                     begin
  108.                         GetMouse (aPoint);    {see where the mouse is pointing now}
  109.                         StdLine (aPoint);    { and draw a line to it}
  110.                     end
  111.                     else DrawOn := false;    {if the mouse btn isn't down, stop drawing}
  112.                 end;
  113.             until quit;
  114.         end;
  115.     end;
  116.         
  117. END.